<?php

namespace App\Http\Controllers\Dashboard;

use App\Models\Area;
use App\Models\User;
use App\Models\WebsiteCity;
use App\Models\WebsiteLoanClass;
use App\Models\WebsiteScheme;
use App\Models\WebsiteUser;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;


class WebSchemeController extends BaseController
{
    public function get(Request $request) {
        $pageSize = (int) $request->input('pageSize', 20);
        $query = WebsiteScheme::with('class')->orderByDesc('id');

        if ($queryName = $request->input('name')) {
            $query->where('name', 'like', "%{$queryName}%");
        }
        if ($classId = $request->class_id) {
            $query->where('class_id', $classId);
        }
        $data = $query->paginate($pageSize);

        foreach ($data as $value){
            $class =  WebsiteLoanClass::where('id',$value->class_id)->select(["id","name"])->first();
            $class->name = $class->name.'-'.$class->id;
            $value->class = $class;
        }
        return response()->json($data);
    }

    public function save(Request $request) {
        $data = $request->all();
        $class = $request->class;
        $model = new WebsiteScheme();
        $model->apply_customer = (string)$request->apply_customer;
        $model->name = (string)$request->name;
        $model->limit = (string)$request->limit;
        $model->interest = (string) $request->interest;
        $model->content = $data['content'];
        $model->url = (string) $request->url;
        $model->class_id = $class['id'];
        $model->created_at = date('Y-m-d H:i:s',time());
        if ($model->save()) {
            return response()->json([
                'code' => 0,
                'msg' => '添加成功',
                'data' => [],
            ]);
        } else {
            return response()->json([
                'code' => 1,
                'msg' => '添加失败',
                'data' => null
            ]);
        }
    }

    public function edit(Request $request, $id)
    {
        $model = WebsiteScheme::find($id);
        if (empty($model)) {
            return response()->json([
                'code' => 1,
                'msg' => '没有数据权限',
                'data' => null
            ]);
        }
        $data = $request->all();
        $class = $request->class;
        $model->name = (string)$request->name;
        $model->apply_customer = (string)$request->apply_customer;
        $model->limit = (string) $request->limit;
        $model->interest = (string) $request->interest;
        $model->content = $data['content'];;
        $model->url = (string) $request->url;
        $model->class_id = $class['id'];
        $model->updated_at = date('Y-m-d H:i:s',time());
        if($model->save()) {
            return response()->json([
                'code' => 0,
                'msg' => '保存成功',
                'data' => $model
            ]);
        } else {
            return response()->json([
                'code' => 1,
                'msg' => '保存失败',
                'data' => null
            ]);
        }
    }

    public function del(Request $request, $id) {
        $model = WebsiteScheme::find($id);
        if (empty($model)) {
            return response()->json([
                'code' => 1,
                'msg' => '数据不存在',
                'data' => null
            ]);
        }

        if($model->delete()) {
            return response()->json([
                'code' => 0,
                'msg' => '删除成功',
                'data' => null
            ]);
        } else {
            return response()->json([
                'code' => 1,
                'msg' => '删除失败',
                'data' => null
            ]);
        }
    }

}
